package com.gopay.common.security.cacert.dao.impl;

import java.io.Serializable;
import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.CustCaCertConstants;
import com.gopay.common.domain.cust.CustCaCert;
import com.gopay.common.security.cacert.dao.CustCaCertQueryDao;
import com.gopay.common.util.DateUtils;

/**
 * 客户证书公共查询dao实现类
 * @CustCaCertQueryDaoImpl.java
 * @author dongdh
 * @2016年2月15日 下午2:39:20  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("custCaCertQueryDao")
public class CustCaCertQueryDaoImpl extends BaseDAOImpl<CustCaCert, Serializable> implements CustCaCertQueryDao {
	private Logger logger = LoggerFactory.getLogger(CustCaCertQueryDaoImpl.class);
    @Override
    public CustCaCert getCustCaCertByCustId(String custId) {
        
        if (StringUtils.isEmpty(custId)) {
            return null;
        }
        
        Map<String, Object> param = new HashMap<>();
        param.put("custId", custId);
        List<CustCaCert> certList = findByHql("from CustCaCert t where t.custId = :custId", param);
        
        if (certList != null && certList.size() > 0) {
            return certList.get(0);
        }
        return null;
    }

	@Override
	public List<CustCaCert> getLimitCustCaCert() {
		/**
		 * 查询出状态为启用，并且已经过期的数据
		 */
		String hql = "from CustCaCert where stat= :stat and "
				   + "TO_CHAR(VALID_TO,'yyyy-MM-dd') <= :currentDate ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("stat", CustCaCertConstants.CUST_CA_CERT_STAT_ACTIVE);
		param.put("currentDate", DateUtils.format(new Date(), "yyyy-MM-dd"));
		
		List<CustCaCert> list = findByHql(hql, param);
		return list;
	}

	@Override
	public CustCaCert getCustCaCertByCidAndUid(String custId, String userId) {
		if (StringUtils.isEmpty(custId)) {
			return null;
		}

		Map<String, Object> param = new HashMap<>();
		param.put("custId", custId);
		param.put("userId", userId);
		List<CustCaCert> certList = findByHql("from CustCaCert t where t.custId = :custId and t.userId = :userId", param);

		if (certList != null && certList.size() > 0) {
			return certList.get(0);
		}
		return null;
	}

	/**
	 * 根据证书主键查询证书信息
	 * zhoutong
	 */
	@Override
    public CustCaCert getCustCaCertById(Serializable certId) {
        CustCaCert cert = find(CustCaCert.class, certId);
        return cert;
	}

	/**
	 * 根据userId和证书状态查询证书列表
	 */
	@Override
    public List<CustCaCert> getCustCaCertListByUserId(String userId, String stat) {
		String hql = "from CustCaCert where stat= :stat and "
				+ " userId = :userId ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("stat", stat);
		param.put("userId", userId);
		
		List<CustCaCert> list = findByHql(hql, param);
		return list;
	}

	@Override
    public List<CustCaCert> getNeedUpdateCustCaCert(String userId){
		String hql = "from CustCaCert where stat= '"+  CustCaCertConstants.CUST_CA_CERT_STAT_ACTIVE +"' and "
				+ " userId = :userId and validTo <= :oneMonthAgo ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", userId);
		param.put("oneMonthAgo", DateUtils.add(new Date(), Calendar.MONTH, -1));
		
		List<CustCaCert> list = findByHql(hql, param);
		return list;
	}

	/**
     * 根据用户ID和序列号获取证书
     * @param userId
     * @param sn
     * @return
     * fanghw
     */
	@Override
    public CustCaCert getByUserIdAndSN(String userId, String sn){
	    String hql = "from CustCaCert where sn= :sn and "
                + " userId = :userId ";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sn", sn);
        param.put("userId", userId);

        List<CustCaCert> list = findByHql(hql, param);

        return list!=null&&list.size()>0?list.get(0):null;
    }

}
